//
//  TryHoneRemoteNotificationHelper.m
//  TryHone
//
//  Created by TryHone on 17/5/26.
//  Copyright © 2017年 TryHone. All rights reserved.
//

#import "JFRemoteNotificationHelper.h"
#import <UIKit/UIKit.h>

@implementation JFRemoteNotificationHelper
//判断是否允许推送通知
+(BOOL)isAllowedNotification{
    if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
    {
        //iOS8
        UIUserNotificationSettings *setting = [[UIApplication sharedApplication] currentUserNotificationSettings];
        if (UIUserNotificationTypeNone != setting.types)
        {
            return YES;
        }
    }
    else
    {
        //iOS7
        UIRemoteNotificationType type = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
        if(UIRemoteNotificationTypeNone != type)
            return YES;
    }
    return NO;
}
//注册通知推送
+(void)registerForRemoteNotification{
    
    UIApplication *application = [UIApplication sharedApplication];
    if([application respondsToSelector:@selector(registerUserNotificationSettings:)])
    {
        UIUserNotificationType notificationTypes = UIUserNotificationTypeBadge | UIUserNotificationTypeSound | UIUserNotificationTypeAlert;
        UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:notificationTypes categories:nil];
        [application registerUserNotificationSettings:settings];
    }
    
#if !TARGET_IPHONE_SIMULATOR
    if ([application respondsToSelector:@selector(registerForRemoteNotifications)])
    {//iOS8 注册APNS
        [application registerForRemoteNotifications];
    }
    else
    {
        UIRemoteNotificationType notificationTypes = UIRemoteNotificationTypeBadge |
        UIRemoteNotificationTypeSound |
        UIRemoteNotificationTypeAlert;
        
        [[UIApplication sharedApplication] registerForRemoteNotificationTypes:notificationTypes];
    }
#endif
    
}


//客户端格式化devicetoken
+(NSString*)formatDeviceToken:(NSData*)data
{
    NSString* tokenString =  data.description;
    NSString* token = [NSString stringWithFormat:@"%@",tokenString];
    token= [token substringFromIndex:1];
    token=[token substringToIndex:token.length-1];
    token = [token stringByReplacingOccurrencesOfString:@" " withString:@""];
    return  token;
    
}
//解除注册推送
+(void)unRegisterForRemoteNotification{
    [[UIApplication sharedApplication] unregisterForRemoteNotifications];
}

@end
